package com.servlet;

import com.util.VerifyCode;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@WebServlet("/getVerifyCode")
public class GetVerifyCode extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedImage img=new BufferedImage(200,60,BufferedImage.TYPE_INT_RGB);
        String chars = VerifyCode.drawRandomImg(200, 60, img);

        //保存验证码到会话对象
        HttpSession session = req.getSession();
        session.setAttribute("verifyCode",chars);

        resp.setContentType("image/png");
        ServletOutputStream out = resp.getOutputStream();
        ImageIO.write(img,"png",out);
        out.close();

    }
}
